import Vue from "vue";
import VueRouter from "vue-router";
import store from "@/store";
import HomeView from "../views/HomeView.vue";
import Login from "../views/Login.vue";
import IndexRecommendInfoManage from "@/views/main/IndexRecommendInfoManage.vue";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: "/home", // 修改为重定向到主页（HomeView）
  },
  {
    path: "/login",
    name: "login",
    component: Login,
  },

  {
    path: "/home",
    name: "home",
    component: HomeView,
    redirect: "/home/recommend",
    children: [
      {
        path: "/home/recommend",
        redirect: "/home/recommend/info",
      },
      {
        path: "/home/recommend/info",
        component: IndexRecommendInfoManage,
        name: "IndexRecommendInfoManage",
      },
      {
        path: "/home/recommend/swiper",
        component: () => import("@/views/main/IndexRecommendManage.vue"),
        name: "IndexRecommendManage",
      },
      // 添加或者编辑轮播页面
      {
        path: "/home/recommend/swiper/addoredit",
        component: () => import("@/views/main/AddOrEditSwiper.vue"),
        name: "AddOrEditSwiper",
      },
      {
        path: "/home/goodsManage",
        component: () => import("@/views/product/GoodsManage.vue"),
        name: "GoodsManage",
      },
      {
        path: "/home/userManage",
        component: () => import("@/views/user/UserManage.vue"),
        name: "UserManage",
      },
    ],
  },
  // {
  //   path: '/about',
  //   name: 'about',
  //   component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  // }
];
const router = new VueRouter({
  mode: "history", // 可以根据需求选择'history'模式或者'hash'模式，这里以'history'为例
  base: process.env.BASE_URL,
  routes,
});

router.beforeEach((to, from, next) => {
  const isLoggedIn = store.getters.isUserLoggedIn; // 获取登录状态
  // console.log(isLoggedIn)
  if (to.name === "home" && !isLoggedIn) {
    next({ name: "login" });
  } else if (to.name === "login" && isLoggedIn) {
    next({ name: "home" });
  } else {
    next();
  }
});

export default router;
